package cn.virens.library.activity;

import android.app.Application;
import android.text.TextUtils;

import org.xutils.x;

/**
 * Created by loioi on 2017/8/11.
 */
public abstract class VApplication extends Application {
    private static final boolean DEBUG = true;

    private static VApplication application;

    public VApplication() {
        VApplication.application = this;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        x.Ext.init(this);
        x.Ext.setDebug(debug());
    }

    protected abstract String _token();

    protected abstract String _server();

    protected abstract String _image(String path);

    /**
     * 判断 - 是否是DEBUG模式
     *
     * @return
     */
    public static boolean debug() {
        return DEBUG;
    }

    /**
     * 判断 - 用户是否登录
     *
     * @return
     */
    public static boolean login() {
        return TextUtils.isEmpty(token());
    }

    /**
     * 用户登录授权码
     *
     * @return
     */
    public static String token() {
        if (VApplication.app() != null) {
            return VApplication.app()._token();
        } else {
            return null;
        }
    }

    /**
     * 服务器地址
     *
     * @return
     */
    public static String server() {
        if (VApplication.app() != null) {
            return VApplication.app()._server();
        } else {
            return null;
        }
    }

    /**
     * 获取相对服务器的图片地址
     *
     * @param path
     * @return
     */
    public static String image(String path) {
        if (VApplication.app() != null) {
            return VApplication.app()._image(path);
        } else {
            return null;
        }
    }

    /**
     * 请求地址
     *
     * @param uri
     * @return
     */
    public static String uri(String uri) {
        if (TextUtils.indexOf(uri, '/') == 0) {
            return (server() + uri);
        } else {
            return (server() + "/" + uri);
        }
    }

    /**
     * 获取 Application对象
     *
     * @return
     */
    public static VApplication app() {
        return VApplication.application;
    }
}
